import {stUrlComponent} from '@/utils/utils.js'
import http from '@/api/http.js'
import orderpay from '@/utils/pay.js'
import {getOrderDetailPage} from '@/utils/toPath.js'
import checkOrderPay from '@/utils/mp_checkOrderPay.js'
//产品订单公共处理方法
const orderEvent = {};

/**
 * 订单创建成功后统一执行事件
 * @param {Object} orderInfo 创建成功后返回的数据 主要使用ordersn和orderid参数
 */
orderEvent.createOrderAfter = function (orderInfo) {
	const{orderid, typeid, status, ordersn} = orderInfo
	if(status == 0)
	{ // 待确认
		uni.redirectTo({
			url:`/pages/order/confirmorder?orderid=${orderid}&ordersn=${ordersn}`
		});
	}
	else if(status == 1)
	{ // 待支付

		// #ifdef MP-WEIXIN || MP-TOUTIAO || MP-ALIPAY
		orderpay(orderInfo,function(){
			checkOrderPay(orderid, ordersn, 10)
		},function(){
			//小程序支付取消失败前往订单详情页
			let url = getOrderDetailPage(typeid,{orderid,ordersn});
			uni.redirectTo({
				url:url
			});
		});
		// #endif
		
		// #ifdef H5
		orderpay(orderInfo);
		// #endif
	}
	else
	{ // 其他状态前往订单详情页面
		let url = getOrderDetailPage(orderInfo.typeid,{orderid:orderInfo.id,ordersn:orderInfo.ordersn});
		uni.redirectTo({
			url:url
		});
	}
}

/**
 * 订单点评页面
 * @param {Object} params 需要参数 typeid:'产品类型id', orderid: '订单id' productaid:'产品自增id', title:'产品标题'
 */
orderEvent.commentOrder = function(params) {
	let path = '/subPackages/comment/add?';
	let url = stUrlComponent(path, params);
	uni.navigateTo({
		url: url
	});
}

/**
 * 取消订单
 * @param {Object} ordersn
 * @param {Function} successfun 
 */
orderEvent.cancelOrder = function(ordersn, successfun) {
	uni.showModal({
		title: '',
		content: '确定取消该订单？',
		showCancel: true,
		confirmColor: '#333333',
		success: res => {
			if (res.confirm) {

				let params = {
					method: 'api/v2/order/cancel_order',
					ordersn: ordersn
				};
				uni.showLoading({
					title: '提交中...'
				})
				http(params).then(res => {
					uni.hideLoading();
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration: 2000
					})
					if (res.status) {
						if (successfun) {
							successfun();
						}
					}
				})
			}
		}
	});
}

/**
 * 取消申请退款
 * @param {Object} ordersn
 * @param {Object} successfun
 */
orderEvent.cancelRefund = function(order_id, successfun) {
	uni.showModal({
		title: '',
		content: '确定取消该订单退款申请？',
		showCancel: true,
		confirmColor: '#333333',
		success: res => {
			if (res.confirm) {
				let params = {
					method: "api/v2/refund/cancel_refund_apply",
					order_id: order_id
				};

				uni.showLoading({
					title: '提交中...'
				})
				http(params).then(res => {
					uni.hideLoading();
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration: 2000
					})
					if (res.status) {
						if (successfun) {
							successfun();
						}
					}
				})
			}
		}
	})
}

/**
 * 退款详情
 * @param {Object} params
 */
orderEvent.refundInfo = function(params) {
	let path = '/subPackages/refund/detail?';
	let url = stUrlComponent(path, params);
	uni.navigateTo({
		url: url
	});
}

/**
 * 申请退款页面
 * @param {Object} params
 */
orderEvent.refundOrder = function(params) {
	let path = '/subPackages/refund/apply?';
	let url = stUrlComponent(path, params);
	uni.navigateTo({
		url: url
	});
}

/**
 * 取消旅游卡预约订单
 * @param {Object} ordersn
 * @param {Function} successfun 
 */
orderEvent.cancelReservation = function(ordersn, successfun) {
	uni.showModal({
		title: '',
		content: '确定取消预约？',
		showCancel: true,
		confirmColor: '#333333',
		success: res => {
			if (res.confirm) {

				let params = {
					method: 'api/v2/card/cancel_order',
					ordersn: ordersn
				};
				uni.showLoading({
					title: '提交中...'
				})
				http(params).then(res => {
					uni.hideLoading();
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration: 2000
					})
					if (res.status) {
						if (successfun) {
							successfun();
						}
					}
				})
			}
		}
	});
}

export default orderEvent
